<?php 
/*-- Project Introduce --*/

/**
 * 视图类 LoginDialog 
 *
 * @access public
 */
class JJ_LoginDialog 
	//<<[UnmodifiableBlock.Start]::Extends And Implements::>>//
	extends JCAT_GtkDialog
	implements JCAT_IViewForm
	//<<[UnmodifiableBlock.End]::Extends And Implements::>>// 
{
	
	/**
	 * 构造函数 
	 *
	 * @access	public
	 * @param	$aParent	JCAT_IViewContainer		视图的父对象(父视图 或 控制器)。注意：视图不应单独存在，必须有一个父对象！ 
	 * @return	void
	 */
	public function JJ_LoginDialog(JCAT_IViewContainer $aParent)
	{
		// 调用父类构造函数 
		$this->JCAT_GtkDialog('LoginDialog ','template.JJ_LoginDialog.glade','JJ_LoginDialog',$aParent) ;

		$this->Initialize() ;	
	}
	
	/**
	 * 初始化 视图 
	 *
	 * @access	protected
	 * @return	void
	 */
	protected function Initialize()
	{
		$this->Create() ;
		$wndMemberPassword = $this->GetChildWidget('ChkRememberPassword') ;
		$wndMemberPassword->set_sensitive(false) ;
		$wndAutoLogin = $this->GetChildWidget('ChkAutoLogin') ;
		$wndAutoLogin->set_sensitive(false) ;
		
		// 载入UI控件
		$sCurrentDirectory = dirname(__FILE__).'/' ;
		$aServerHostList = JCAT_Package::ImportInstance($sCurrentDirectory.'Instances.UICtrl/instance.SelServerHostList.php',null,$this) ;
		$aUsername = JCAT_Package::ImportInstance($sCurrentDirectory.'Instances.UICtrl/instance.SelUsername.php',null,$this) ;
		$aPassword = JCAT_Package::ImportInstance($sCurrentDirectory.'Instances.UICtrl/instance.EntPassword.php',null,$this) ;
		$aRememberPassword = JCAT_Package::ImportInstance($sCurrentDirectory.'Instances.UICtrl/instance.ChkRememberPassword.php',null,$this) ;
		$this->AddUICtrl($aServerHostList) ;
		$this->AddUICtrl($aUsername) ;
		$this->AddUICtrl($aPassword) ;
		$this->AddUICtrl($aRememberPassword) ;
		
		
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		$this->GetChildWidget('ChkAutoLogin')->set_active($aSetting->GetItem('/JJ/Login','AutoLogin')) ;
		
		// 加载 用户记忆
		$this->LoadUserMemery() ;
		
		// 加载可用服务器列表
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		$this->arrServerHostList = $aSetting->GetItem('/Jabber','ServerHostList') ;
		foreach($this->arrServerHostList as $sServerHost)
		{
			$aServerHostList->AddOption($sServerHost,$sServerHost) ;
		}
		$aServerHostList->SetDataByExchange($aSetting->GetItem('/Jabber','ServerHost')) ;
		
		// 隐藏服务器 菜单
		if($aSetting->GetItem('/Jabber','HideServerHostList'))
		{
			$this->GetChildWidget('SelServerHostList')->set_visible(false) ;
			$this->GetChildWidget('LabServerHost')->set_visible(false) ;
		}
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @param 	$bAutoDestroy=false	bool		自动销毁
	 * @return	void
	 */
	public function DoDialog($bAutoDestroy=false)
	{
		if( $this->GetChildWidget('ChkAutoLogin')->get_active() )
		{
			return Gtk::RESPONSE_OK ;
		}
		
		else 
		{
			return parent::DoDialog($bAutoDestroy) ;
		}
	}
	
	/**
	 * 载入 记忆的用户信息
	 *
	 * @access	public
	 * @param	$Parameter
	 * @return	void
	 */
	public function LoadUserMemery()
	{
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		$this->arrUserList = $aSetting->GetItem('/JJ/Login','UserList') ;
		$sLastLoginedUser = $aSetting->GetItem('/JJ/Login','LastLoginedUser') ;
		
		$aSelUsername = $this->GetUICtrl('SelUsername') ;
		
		// 载入用户名列表
		foreach ($this->arrUserList as $sUsername=>$sPassword)
		{$sPassword ;
			$aSelUsername->AddOption($sUsername,$sUsername) ;
		}
		
		// 设置上次 选择 项
		if( isset($this->arrUserList[$sLastLoginedUser]) )
		{
			$aSelUsername->SetDataByExchange($sLastLoginedUser) ;
		}
	}

	/**
	 * What's this Method ?
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnOk() 
	{
		$aSetting = JJ_Setting::GetGlobalInstance() ;
				
		// 设置服务器
		if( !$aSetting->GetItem('/Jabber','HideServerHostList') )
		{
			$sServerHost = $this->GetUICtrlExchangeData('SelServerHostList') ;
			$sServerHost = strtolower($sServerHost) ;
		
			// 添加到可用服务器列表
			if( !in_array($sServerHost,$this->arrServerHostList) )
			{
				$this->arrServerHostList[] = $sServerHost ;
				$aSetting->SetItem('/Jabber','ServerHostList',$this->arrServerHostList) ;
			}
		}
		
		parent::OnOk() ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnServerHostChanged()
	{
		$sServerHost = $this->GetUICtrlExchangeData('SelServerHostList') ;
		$sServerHost = strtolower($sServerHost) ;
		
		// 保存
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		$aSetting->SetItem('/Jabber','ServerHost',$sServerHost) ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnRegister()
	{
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		$sWebRegisterUrl = $aSetting->GetItem('/JJ','WebRegisterUrl') ;
		
		// 网页注册
		if($sWebRegisterUrl)
		{
			jj::OpenWebpage($sWebRegisterUrl) ;
		}
		
		// 注册 对话框
		else 
		{
			// 先隐藏登录窗口
			$this->ShowView(false) ;
			
			$aController = $this->GetParentController() ;
			$aMainController = $aController->GetTopController() ;
			
			$aRegister = new JJ_Register($aMainController) ;
			if( $aRegister->MainRun() )
			{
				$sUsername = $aRegister->GetNewUsername() ;
				$sPassword = $aRegister->GetNewPassword() ;
				
				$this->SetUICtrlExchangeData('SelUsername',$sUsername) ;
				$this->SetUICtrlExchangeData('EntPassword',$sPassword) ;
			}
			
			
			// 恢复登录窗口
			$this->ShowView(true) ;
		}
	}
	
	
	/**
	 * 记录 用户账号
	 *
	 * @access	public
	 * @param	$Parameter
	 * @return	void
	 */
	public function SaveUserMemery()
	{
		$sPassword = $this->GetUICtrlExchangeData('EntPassword') ;
		$sUsername = $this->GetUICtrlExchangeData('SelUsername') ;
		$sUsername = strtolower($sUsername) ;
		$bAutoLogin = false ;
		
		// 记录账号
		if( $this->GetChildWidget('ChkRememberUser')->get_active() )
		{			
			// 记录 密码
			if( $this->GetChildWidget('ChkRememberPassword')->get_active() )
			{
				$this->arrUserList[$sUsername] = base64_encode($sPassword) ;
				
				// 自动登录
				if( $this->GetChildWidget('ChkAutoLogin')->get_active() )
				{
					$bAutoLogin = true ;
				}
				
			}
			
			// 清除密码
			else
			{
				$this->arrUserList[$sUsername] = '' ;
			}
		}
		
		// 清楚 账号记录
		else
		{
			unset( $this->arrUserList[$sUsername] ) ;
		}
		
		// 设置 保存
		$aSetting = JJ_Setting::GetGlobalInstance() ;
		$aSetting->SetItem('/JJ/Login','UserList',$this->arrUserList) ;
		$aSetting->SetItem('/JJ/Login','LastLoginedUser',$sUsername) ;
		$aSetting->SetItem('/JJ/Login','AutoLogin',$bAutoLogin) ;
		
		// 立即保存
		$aKey = $aSetting->GetKey('/JJ/Login') ;
		$aKey->Save() ;
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnUsernameChanged()
	{
		$sUsername = strtolower($this->GetUICtrlExchangeData('SelUsername')) ;
		if( isset($this->arrUserList[$sUsername]) )
		{
			$this->GetChildWidget('ChkRememberUser')->set_active(true) ;

			$sPassword = base64_decode($this->arrUserList[$sUsername]) ;
			if($sPassword)
			{
				$this->SetUICtrlExchangeData('EntPassword',$sPassword) ;
				$this->GetChildWidget('ChkRememberPassword')->set_active(true) ;
			}
		}
	}

	/**
	 * 记住用户
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnToggledMemberUser(GtkToggleButton $wndMemberUser)
	{
		$wndMemberPassword = $this->GetChildWidget('ChkRememberPassword') ;
		$wndMemberPassword->set_sensitive($wndMemberUser->get_active()) ;
		if(!$wndMemberUser->get_active())
		{
			$wndMemberPassword->set_active(false) ;
		}
	}
	
	/**
	 * Description
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnToggledAutoLogin(GtkToggleButton $wndMemberPassword)
	{
		$wndChkAutoLogin = $this->GetChildWidget('ChkAutoLogin') ;
		$wndChkAutoLogin->set_sensitive($wndMemberPassword->get_active()) ;
		if(!$wndMemberPassword->get_active())
		{
			$wndChkAutoLogin->set_active(false) ;
		}
	}
	
	/**
	 * Description
	 * 
	 * @access	private
	 * @var		array
	 */
	private $arrUserList = array() ;
	/**
	 * Description
	 * 
	 * @access	private
	 * @var		array
	 */
	private $arrServerHostList = array() ;
	
}

?>